package com.fjnldx.mes.service.alarm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjnldx.mes.domain.domainReq.alarm.EditAlarmNoticeReq;
import com.fjnldx.mes.domain.pojo.alarm.AlarmNotice;
import com.fjnldx.mes.mapper.alarm.AlarmNoticeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 告警通知服务
 */
@Service
public class AlarmNoticeService {
    @Resource
    private AlarmNoticeMapper alarmNoticeMapper;

    /**
     * 更新告警通知
     */
    public void UpdateAlarmNotice(Integer alarmId, List<EditAlarmNoticeReq> param) throws Exception {
        try {
            List<AlarmNotice> notices = getAlarmNotice(alarmId);
            for (EditAlarmNoticeReq editInfo : param) {
                AlarmNotice notice = null;
                List<AlarmNotice> filterNotice = notices.stream().filter(x -> x.getId() == (editInfo.getId() == null ? 0 : editInfo.getId())).collect(Collectors.toList());
                if (filterNotice.size() > 0) notice = filterNotice.get(0);
                if (notice == null) {
                    notice = new AlarmNotice();
                    notice.setAlarmId(alarmId);
                    notice.setReceiversJson(editInfo.getReceiversJson());
                    notice.setReceiverMethodsJson(editInfo.getReceiverMethodsJson());
                    notice.setMessageTemplate(editInfo.getMessageTemplate());
                    notice.setReceiveUrl(editInfo.getReceiveUrl());
                    notice.setCreateTime(LocalDateTime.now());
                    alarmNoticeMapper.insert(notice);
                } else {
                    //更新
                    notice.setReceiversJson(editInfo.getReceiversJson());
                    notice.setReceiverMethodsJson(editInfo.getReceiverMethodsJson());
                    notice.setMessageTemplate(editInfo.getMessageTemplate());
                    notice.setReceiveUrl(editInfo.getReceiveUrl());
                    alarmNoticeMapper.updateById(notice);
                }
            }
            //删除
            List<Integer> paramIds = param.stream().map(EditAlarmNoticeReq::getId).collect(Collectors.toList());
            List<Integer> currentIds = notices.stream().map(AlarmNotice::getId).collect(Collectors.toList());
            List<Integer> deleteIds = currentIds.stream().filter(id -> !paramIds.contains(id)).collect(Collectors.toList());
            if (deleteIds.size() > 0)
                alarmNoticeMapper.deleteBatchIds(deleteIds);
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 获取告警通知
     *
     * @param id 告警配置标识
     * @return 告警规则集合响应值
     */
    public List<AlarmNotice> getAlarmNotice(Integer id) throws Exception {
        try {
            //查询配置规则
            QueryWrapper<AlarmNotice> alarmNoticeQueryWrapper = new QueryWrapper<>();
            alarmNoticeQueryWrapper.eq("alarmId", id);
            return alarmNoticeMapper.selectList(alarmNoticeQueryWrapper);
        } catch (Exception e) {
            throw new Exception();
        }
    }
}
